package zxs.sino.elite.hub.model.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户模型类，实现Spring Security的UserDetails接口
 *
 * @author zhang
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserModel implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    private Long userId;

    private String authKey;

    private String role;
    
    private Set<String> permissions = new HashSet<>();


    public UserModel(Long userId, String authKey, String role) {
        this.userId = userId;
        this.authKey = authKey;
        this.role = role;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 将权限字符串转换为Spring Security的GrantedAuthority对象
        Set<GrantedAuthority> authorities = new HashSet<>();
        if (permissions != null) {
            permissions.forEach(permission -> 
                authorities.add(() -> permission)
            );
        }
        return authorities;
    }

    @Override
    public String getPassword() {
        // 我们使用JWT，不需要密码
        return null;
    }

    @Override
    public String getUsername() {
        // 使用authKey作为用户名
        return authKey;
    }

    @Override
    public boolean isAccountNonExpired() {
        // 账户永不过期
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        // 账户永不锁定
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        // 凭证永不过期
        return true;
    }

    @Override
    public boolean isEnabled() {
        // 账户始终启用
        return true;
    }
}
